app.controller('myshopper', function ($scope, $http) {

    var token = localStorage.getItem('token');
    $scope.shopInfo = [];

    //查看我的购物车
    $scope.myshopper = function () {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }
        if (token) {

            $http({
                method: 'get',
                url: app.netPath + '/sc/show',
                headers: {Authorization: token},
                params: {scType: 1}
            }).then(function (res) {
                // 删除加载提示符
                infiniteScrollPreloader.hide();
                if (res.data.data == undefined) {
                    $scope.listSize = 0
                }
                if (!res.data.error) {
                    console.log(res.data.data);
                    $scope.shopInfo = res.data.data;
                    $scope.shopInfo.shopNum = res.data.data.length;


                    for (var i = 0; i < $scope.shopInfo.length; i++) {
                        $scope.shopInfo[i].aClass.classImg = app.netPath + '/resources/classImg/' + $scope.shopInfo[i].aClass.classImg;
                        app.buildImageUrl($scope.shopInfo[i].aClass,'classImg',function () {
                            $scope.$digest();
                        });

                    }
                }
                else {

                }
            }, function () {
                $.toast('网络走丢了！')
            })
        }

    };


    //得到scId，删除收藏
    $scope.getScId = function (scId) {

        $.confirm('确认删除此收藏？', '', function () {

            $http({
                method: 'get',
                url: app.netPath + '/sc/delete',
                headers: {Authorization: token},
                params: {scIds: scId, scType: 0}
            }).then(function (res) {
                if (!res.data.error) {
                    location.href = '#collection';
                    $.toast(res.data.msg)
                }
                else {
                    $.toast(res.data.msg);
                }
            })
        }, function () {
            return;
        })

    };

    //得到classId
    $scope.getClassId = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';

    };


    // 删除所选购物车
    $scope.deleteSelected = function () {
        if ($scope.shopInfo.length == 0) {
            return;
        }
        var shopSelected = '';
        for (var i = 0; i < $scope.shopInfo.length; i++) {
            console.log($scope.shopInfo[i].isPreDelete);
            if ($scope.shopInfo[i].isPreDelete) {
                shopSelected += $scope.shopInfo[i].scId + ','
            }
            console.log(shopSelected + ">>>")
        }
        shopSelected = shopSelected.substring(0, shopSelected.length - 1);
        if (shopSelected.length == 0) {
            $.toast('你没有选中任何课程');
            return;
        }
        $.confirm('删除所有选择的课程？', '', function () {
            $http({
                method: 'GET',
                url: app.netPath + '/sc/delete',
                headers: {Authorization: token},
                params: {scIds: shopSelected, scType: 1}
            }).then(function (res) {
                console.log();
                if (!res.data.error) {
                    $.toast('删除成功');

                    location.href = '#myshopper'

                }
                else {
                    $.toast(res.data.msg)
                }
            });
        }, function () {
            return;
        });
    };

    //马上购买课程

    $scope.buyNow = function (classId, scId) {
        if (!token) {
            $.toast("请先登录");
            location.href = '#user-login';

            return;
        }

        if (token) {

            $http({
                method: 'get',
                url: app.netPath + '/order/add',
                headers: {Authorization: token},
                params: {classId: classId}
            }).then(function (res) {
                console.log(res.data);
                if (!res.data.error) {
                    //返回订单生成成功
                    //查看关于该课程是订单
                    $http({
                        method: 'get',
                        url: app.netPath + '/order/show/noworderinfo',
                        headers: {Authorization: token},
                        params: {classId: classId}
                    }).then(function (res) {
                        if (!res.data.error) {
                            console.log(res.data);
                            $scope.orderInfo = res.data.data;
                            sessionStorage.setItem('orderId', $scope.orderInfo.orderId);

                            //购买课程

                            $.confirm("是否购买此课程？", "", function () {

                                var orderId = sessionStorage.getItem('orderId');
                                $http({
                                    method: 'get',
                                    url: app.netPath + '/order/payorder',
                                    headers: {Authorization: token},
                                    params: {orderId: orderId}
                                }).then(function (res) {
                                    if (!res.data.error) {
                                        console.log(res.data + ">>>");
                                        //支付成功后清除购物车
                                        $.toast("支付成功");
                                        $http({
                                            method: 'GET',
                                            url: app.netPath + '/sc/delete',
                                            headers: {Authorization: token},
                                            params: {scIds: scId, scType: 1}
                                        }).then(function (res) {
                                            console.log();
                                            if (!res.data.error) {

                                                location.href = '#myshopper'
                                            }
                                            else {
                                                $.toast(res.data.msg)
                                            }
                                        });
                                    }
                                    else {
                                        $.toast(res.data.msg)

                                    }

                                })
                            }, function () {
                                $.toast("交易取消,可在我的订单查看详情")
                            });

                        }
                    });
                } else if (res.data.error == 102) {
                    //返回订单生成成功
                    //查看关于该课程是订单
                    $http({
                        method: 'get',
                        url: app.netPath + '/order/show/noworderinfo',
                        headers: {Authorization: token},
                        params: {classId: classId}
                    }).then(function (res) {
                        if (!res.data.error) {
                            console.log(res.data);
                            $scope.orderInfo = res.data.data;
                            sessionStorage.setItem('orderId', $scope.orderInfo.orderId);

                            //购买课程

                            $.confirm("是否购买此课程？", "", function () {

                                var orderId = sessionStorage.getItem('orderId');
                                $http({
                                    method: 'get',
                                    url: app.netPath + '/order/payorder',
                                    headers: {Authorization: token},
                                    params: {orderId: orderId}
                                }).then(function (res) {
                                    if (!res.data.error) {
                                        console.log(res.data + ">>>");
                                        //支付成功后清除购物车
                                        $.toast("支付成功");

                                        $http({
                                            method: 'GET',
                                            url: app.netPath + '/sc/delete',
                                            headers: {Authorization: token},
                                            params: {scIds: scId, scType: 1}
                                        }).then(function (res) {
                                            console.log();
                                            if (!res.data.error) {

                                                location.href = '#myshopper'
                                            }
                                            else {
                                                $.toast(res.data.msg)
                                            }
                                        });
                                    }
                                    else {
                                        $.toast(res.data.msg)

                                    }

                                })
                            }, function () {
                                $.toast("交易取消,可在我的订单查看详情")
                            });

                        }
                    });
                }
                else {
                    $.toast(res.data.msg);
                    return;

                }

            })
        }
    };

});
